
#include "GuiMain.hpp"


namespace GUI
{
	const WCHAR WINDOW_CLASS_NAME [] = L"NetworkWindow";
	const WCHAR WINDOW_NAME[] = L"Network Scan";

	const ULONG WM_USER_NETWORK = WM_USER + 1;

	HWND GuiMain::hwndMain = 0;


	GuiMain::GuiMain()
	{
		HRESULT hr = CoInitialize(NULL);

		DWORD error;

		WNDCLASSEX wcx; 
		wcx.cbSize = sizeof(wcx);									// size of structure 
		wcx.style = CS_HREDRAW | CS_VREDRAW;						// redraw if size changes 
		wcx.lpfnWndProc = (WNDPROC) &GuiMain::WndProc;								// points to window procedure 
		wcx.cbClsExtra = 0;											// no extra class memory 
		wcx.cbWndExtra = 0;											// no extra window memory 
		wcx.hInstance = GetModuleHandle(NULL);									// handle to instance 
		wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);				// predefined app. icon 
		wcx.hCursor = LoadCursor(NULL, IDC_ARROW);					// predefined arrow 
		wcx.hbrBackground = (HBRUSH) COLOR_WINDOW;   // white background brush 
		wcx.lpszMenuName =  NULL;									// name of menu resource 
		wcx.lpszClassName = WINDOW_CLASS_NAME;						// name of window class 
		wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

		// Register the window class.
	    RegisterClassEx(&wcx); 

		error = GetLastError();

		 

		// Create the main window. 

		this->hwndMain  = CreateWindowEx(
			WS_EX_OVERLAPPEDWINDOW,							// no extended styles 
			WINDOW_CLASS_NAME,			// name of window class 
			WINDOW_NAME,			    // title-bar string 
			WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,	// overlapped window  // horizontal scroll bar // vertical scroll bar
			CW_USEDEFAULT,          // default horizontal position  
			CW_USEDEFAULT,          // default vertical position    
			CW_USEDEFAULT,          // default width                
			CW_USEDEFAULT,          // default height               
			(HWND) NULL,            // no parent or owner window    
			(HMENU) NULL,           // class menu used              
			GetModuleHandle(NULL),              // instance handle              
			NULL);                  // no window creation data  

		error = GetLastError();

		Init();
	}

	GuiMain::~GuiMain()
	{
		delete dialogCtrl;
		CoUninitialize();
	}

	void GuiMain::Init()
	{
		 dialogCtrl = new DialogCtrl(hwndMain);

	}

	WPARAM GuiMain::run(int nCmdShow)
	{
		if (IsWindow(hwndMain))
		{
			ShowWindow(hwndMain, nCmdShow); 
			UpdateWindow(hwndMain); 

			dialogCtrl->run(1);

			MSG msg; 
			while (GetMessage(&msg, (HWND) NULL, 0, 0))
			{ 
				TranslateMessage(&msg); 
				DispatchMessage(&msg); 
			}
			return msg.wParam; 
		}
		return 0;
	}

	LRESULT CALLBACK GuiMain::WndProc(
	HWND hwnd,        // handle to window
	UINT uMsg,        // message identifier
	WPARAM wParam,    // first message parameter
	LPARAM lParam)    // second message parameter
{ 

	switch (uMsg) 
	{			

	case WM_CREATE:
		// Initialize the window. 
		return 0; 

	case WM_TIMER:
		{
			/*if ( wParam == 0 ) {

				int pos = SendMessage(hProgress, PBM_GETPOS, 0, 0 ) + 1;

				SendMessage(hProgress, PBM_SETPOS, pos, 0 );

				if ( pos == 60 ) {

					SendMessage(hProgress, PBM_SETPOS, 0, 0 );
				}
			}
			return 0;*/
		}
	break;

	case WM_PAINT: 
		// Paint the window's client area.
		PAINTSTRUCT paint_structure;
		BeginPaint(hwnd, &paint_structure);
		EndPaint(hwnd, &paint_structure);  
		return 0; 

	case WM_SIZE: 
		// Set the size and position of the window. 
		return 0; 

	case WM_DESTROY: 
		// Clean up window-specific data objects.
		PostQuitMessage(0);
		return 0; 

	case WM_USER_NETWORK:
		return 0;

	case WM_COMMAND:
		return 0;
		// 
		// Process other messages. 
		// 

	default: 
		return DefWindowProc(hwnd, uMsg, wParam, lParam); 
	} 
	return 0; 
} 
}